# coding=gbk

from datetime import date, timedelta
from DataAccess.DBConnFactory import DBConnFactory
from Import.TradeImport import *
from Import.PositionImport import *
from Position.CalcDailyPosition import *
from Recover.PositionRecover import *
from Config.ImportConfig import *

def test_case4():
	conn = DBConnFactory().get_db_connection('PKEDB')
	cursor = conn.cursor()
	
	#clear db data
	clear_all()
	print 'info, clear all data complete.'

	#add portfolio info entry
	sql_text = '''delete from portfolio_info where portfolio_id='SCTestP' '''
	cursor.execute(sql_text)
	conn.commit()
	sql_text = '''insert into portfolio_info(portfolio_id,"name",inception_date,inception_val,currency,manager,"type",cash_account) values 
				('SCTestP','SCTestP',TO_DATE('2012-05-24','yyyy-mm-dd'),0,'USD','XieBing','PUBLIC',0)'''
	cursor.execute(sql_text)
	conn.commit()
	print 'info, add test portfolio info complete.'
	
	sql_text = '''insert into position 
				(ref_date,portfolio_id,ticker,security_type,amount,avg_cost_price,price_currency)
				values(TO_DATE(:1,'yyyy-mm-dd'),:2,:3,:4,:5,:6,:7) '''				
	v =			[('2012-05-24','SCTestP','9999999','CASH',0,1.0,'USD'), 
				('2012-05-24','SCTestP','9999999','CASH',0,1.0,'CNY'), 
				('2012-05-24','SCTestP','9999999','CASH',0,1.0,'HKD'),
				('2012-05-24','SCTestP','Pnote_200012','PNOTE',1111,1.0,'HKD'),
				('2012-05-24','SCTestP','Pnote_300256','PNOTE',2345,2.0,'USD'),
				('2012-05-24','SCTestP','Pnote_900933','PNOTE',-1555,3.0,'USD')]
	cursor.executemany(sql_text, v)
	
	sql_text = '''insert into pnote_info values(:1,:2,:3,:4) '''
	v =			[('Pnote_200012','200012','HKD','HKD'), 
				('Pnote_300256','300256','USD','CNY'), 
				('Pnote_900933','900933','USD','USD')]
	cursor.executemany(sql_text, v)
	conn.commit()
	print 'info, add initial position complete.'
	#return
		
	trade_date = date(2012, 5, 25)			
	update_position_driven_by_dividend(trade_date)
	update_position_driven_by_transaction(trade_date)
	update_position_driven_by_repo_expire(trade_date)
	print 'info, calculate 2012-05-25 position complete.'
	
	sql_text = '''delete from portfolio_info where portfolio_id='SCTestP' '''
	cursor.execute(sql_text)
	conn.commit()
	

	
	